package org.abettor.pushbox.activity;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.abettor.pushbox.config.Config;
import org.abettor.pushbox.db.PushBoxMapBeanManage;
import org.abettor.pushbox.map.Boxmap;
import org.abettor.pushbox.map.BoxmapUtil;
import org.abettor.pushbox.map.Step;
import org.abettor.pushbox.upload.UploadMap;
import org.abettor.pushbox.upload.UploadMap.UpdateUploadMap;
import org.abettor.pushbox.util.FileUtil;
import org.abettor.pushbox.R;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class SelfShowResultActivity extends AbstractShowResultActivity {
	@SuppressWarnings("unused")
	private final int NOT_UPLOADED = 0, UPLOADED = 1;// 用来指示是否已经上传过
	private PushBoxMapBeanManage manage;
	private int map_id = -1;// 地图的id
	private String filename;// 文件的名字
	private final int REPUSHMAP_ID = Menu.FIRST;//
	private final int UPLOAD_MAP_ID = Menu.FIRST + 5;// 上传地图
	private int is_uploaded = 0;// 判断是否已经上传过
	private UploadMap uploadMapBean = null;

	private String readFolder() {
		return Config.getSelfBasePath();
	}

	/**
	 * 创建系统菜单
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		boolean result = super.onCreateOptionsMenu(menu);
		menu.add(0, REPUSHMAP_ID, 1, R.string.restart_game).setIcon(
				R.drawable.reset);
		return result;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case REPUSHMAP_ID:// 重新开始游戏
			restart();
			break;
		case UPLOAD_MAP_ID:
			uploadMap(map_id);
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	//上传地图的操作
	public void uploadMap(final int id){
		String filename = manage.getFileName(id);
		// 读取图片的操作
		try {
			byte[] data = FileUtil.readFileByPath(Config.getSelfBasePath()
					+ File.separator + filename + ".map");
			UpdateUploadMap in = new UpdateUploadMap(){
				@Override
				public void update(int id) {
					manage.updateUploadStateSuccess(map_id, id);
					//重新读取状态
					is_uploaded = manage.getUploadedStatus(map_id);
				}
			};
			uploadMapBean.uploadMap(data, in);
		} catch (IOException e) {
		}
	}
	//重新开始游戏
	private void restart(){
		Intent in = new Intent();
		Bundle extras = new Bundle();
		extras.putInt("_id", map_id);
		in.putExtras(extras);
		in.setClass(SelfShowResultActivity.this, SelfPushBoxActivity.class);
		startActivity(in);
		SelfShowResultActivity.this.finish();
	}
	/**
	 * 自做系统菜单
	 */
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		if (is_uploaded != UPLOADED) {
			MenuItem uploaded = menu.findItem(UPLOAD_MAP_ID);
			if (uploaded == null) {
				menu.add(0, UPLOAD_MAP_ID, 6, R.string.upload_map).setIcon(
						R.drawable.upload);
			}
		} else {
			MenuItem uploaded = menu.findItem(UPLOAD_MAP_ID);
			if (uploaded != null) {// 上传后还有这个选项，把它从列表中删除掉
				menu.removeItem(UPLOAD_MAP_ID);
			}
		}
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	protected Boxmap initMap() {
		String folder = readFolder() + File.separator + filename + ".map";
		try {
			byte[] buf = readFileByPath(folder);
			return BoxmapUtil.paraArrayToMap(buf);
		} catch (IOException e) {
			return null;
		}
	}

	@Override
	protected List<Step> initStep() {
		String folder = readFolder() + File.separator + filename + ".step";
		try {
			byte[] buf = readFileByPath(folder);
			return BoxmapUtil.paraArrayToStep(buf);
		} catch (IOException e) {
			return null;
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		init();
		super.onCreate(savedInstanceState);
	}

	private void init() {
		Intent intent = getIntent();
		uploadMapBean = new UploadMap(this);
		manage = new PushBoxMapBeanManage(this);
		Bundle extras = intent.getExtras();
		map_id = extras.getInt("_id");// 获得数据的id
		filename = manage.getFileName(map_id);
		is_uploaded = manage.getUploadedStatus(map_id);
	}

}
